// view/index/jiankongxq/jiankongxq.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
    /**
     * 页面的初始数据
     */
    ...request,
    data: {
        yuming: yuming,
        heights: "",
        title: '监控详情',
        buttonClicked: false,
        jinghe_id: '',
        Xq: '',
        tongnum: '',
        // list: [{
        //     time: '2022-3-25',
        //     zpsl: 15,
        //     images: ['', '', '', '', '', '']
        //   },
        //   {
        //     time: '2022-3-24',
        //     zpsl: 10,
        //     images: ['', '', '', '', '', '']
        //   },
        //   {
        //     time: '2022-3-23',
        //     zpsl: 16,
        //     images: ['', '', '']
        //   },
        //   {
        //     time: '2022-3-22',
        //     zpsl: 12,
        //     images: ['', '', '', '', '', '']
        //   }
        // ]
        list: [],
        page: 1,
        gongwu: false,
        videoURL: '',
        isQie: false,
        isAn: true,
        qiehuan: '切换源',
        isjing_tong: false,
        qiehuan_list: [], //切换源list
        qieyuan_guo: '',
        qieyuan_index: '',
    },
    tongnum(e) {
        this.setData({
            tongnum: e.detail.value
        })
    },
    // 泾河新城切换源
    jing_qie() {
        this.setData({
            isjing_tong: !this.data.isjing_tong,
            tongnum: ''
        })
    },
    bindqie(e) {
        console.log(e)
        var index = e.detail.value
        this.setData({
            qieyuan_index: index,
            qieyuan_guo: this.data.qiehuan_list[index].channelNo
        })
        this.dian_qieyuan()
    },
    // 点击切换源原接口
    dian_qieyuan() {
        this.myRequest(yuming + 'Monitor/jhxc_monitor_savechannelNo', {
            id: this.data.id,
            shopId: wx.getStorageSync('shopId'),
            channelNo: this.data.qieyuan_guo
        }, "POST").then(res => {
            wx.hideLoading();
            this.setData({
                isjing_tong: false
            })
            if (res.data.code == 200) {
                this.setData({
                    videoURL: ''
                })
                this.getXq()
            }
        })
    },

    // 切换源列表接口
    qie_list() {
        this.myRequest(yuming + 'Monitor/jhxc_monitor_seechannelNo', {
            id: this.data.id,
            shopId: wx.getStorageSync('shopId'),
        }, "POST").then(res => {
            console.log(res)
            this.setData({
                qiehuan_list: res.data.data,
                qieyuan_guo: res.data.data[0].channelNo
            })
            this.dian_qieyuan()
        })
    },
    // 通道确定
    jing_que() {
        if (this.data.tongnum == '') {
            wx.showToast({
                title: '请输入通道号',
                icon: 'error'
            })
        } else {
            this.myRequest(yuming + 'Monitor/jhxc_monitor_savechannelNo', {
                id: this.data.id,
                shopId: wx.getStorageSync('shopId'),
                channelNo: this.data.tongnum
            }, "POST").then(res => {
                wx.hideLoading();
                this.setData({
                    isjing_tong: false
                })
                if (res.data.code == 200) {
                    this.setData({
                        videoURL: ''
                    })
                    this.getXq()
                }
            })
        }
    },
    // 放大
    fangda(e) {
        var img = e.currentTarget.dataset.url
        var list = e.currentTarget.dataset.list
        var arr = []
        for (let i in list) {
            arr.push(list[i].image); //属性
        }
        wx.previewImage({
            current: img,
            urls: arr
        })
    },
    getXq() {
        let that = this;
        wx.showLoading({
            title: '加载中',
            icon: 'none'
        })
        this.myRequest(yuming + 'Monitor/shop_monitor_info', {
            id: that.data.id,
            shopId: wx.getStorageSync('shopId')
        }, "POST").then(res => {
            wx.hideLoading();
            if (res.data.code == 200) {
                this.setData({
                    Xq: res.data.data,
                    videoURL: res.data.data.encryption_low
                })
            }
        })
    },
    // 查看更多
    lookmore() {
        shuzong = this.data.list
        this.setData({
            page: (this.data.page) + 1
        })
        this.getzpList()
    },
    getzpList() {
        let that = this;
        wx.showLoading({
            title: '加载中',
            icon: 'none'
        })
        this.myRequest(yuming + 'Monitor/shop_monitor_zhuapai_list', {
            shopId: wx.getStorageSync('shopId'),
            id: that.data.id,
            page: that.data.page
        }, "POST").then(res => {
            wx.hideLoading();
            if (res.data.code == 200) {
                shu = res.data.data
                if (this.data.page != 1) {
                    if (shu.length != 10) {
                        wx.showToast({
                            title: '没有更多数据了~',
                            icon: "none"
                        })
                        this.setData({
                            gongwu: true
                        })
                    }
                }
                this.setData({
                    list: shuzong.concat(shu)
                })
            }
        })
    },
    zhuapai() {
        let that = this;
        that.setData({
            buttonClicked: true
        })
        wx.showLoading({
            title: '抓拍中',
            icon: 'none'
        })
        this.myRequest(yuming + 'Monitor/shop_zhuapai_monitor', {
            id: that.data.id,
        }, "POST").then(res => {
            wx.hideLoading();
            if (res.data.code == 200) {
                setTimeout(() => {
                    this.setData({
                        buttonClicked: false
                    })
                }, 3000)
                this.getzpList();
            } else {
                that.setData({
                    buttonClicked: false
                })
            }
        })
    },
    // 展示选项
    qiehuanyuan() {
        this.setData({
            // videoURL: '',
            isQie: !this.data.isQie
        })
    },
    quxiao() {
        this.setData({
            isQie: false
        })
    },
    // 切换对应的源
    qieyuan(e) {
        let url = e.currentTarget.dataset.url
        let name = e.currentTarget.dataset.name
        this.setData({
            videoURL: url,
            isQie: false,
            qiehuan: name
        })
    },
    // 配置监控
    peizhi(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/page_one/jiankongpz/jiankongpz?id=' + id,
        })
    },
    shua(e) {
        let id = e.currentTarget.dataset.id
        if (this.data.isAn) {
            this.setData({
                isAn: false
            })
            this.shuaxin(id);
        } else {
            wx.showToast({
                title: '请勿频繁请求',
                icon: 'none'
            })
        }
    },
    // 刷新监控
    shuaxin(id) {
        wx.showLoading({
            title: '刷新中',
            icon: 'none'
        })
        this.myRequest(yuming + 'Monitor/shop_save_monitor_url', {
            id,
        }, "POST").then(res => {
            wx.hideLoading();
            if (res.data.code == 200) {
                this.getjiankongList();
                setTimeout(() => {
                    this.setData({
                        isAn: true
                    })
                }, 60000)
            }
        })
    },
    backs() {
        wx.navigateBack({
            delta: 0
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            heights: wx.getMenuButtonBoundingClientRect().top + 5,
            title: options.title,
            id: options.id
        })
        this.setData({
            videoURL: options.url
        })
        shuzong = []
        shu = []
        this.getXq();
        this.getzpList();
        this.qie_list()
        // this.getzp();
        // this.getUrl();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            jinghe_id: wx.getStorageSync('jinghe_id')
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
})